package com.ego.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUIDatagrid;
import com.ego.commons.pojo.EgoResult;
import com.ego.commons.utils.IDUtils;
import com.ego.dubbo.service.TBItemDubboService;
import com.ego.dubbo.service.TbItemParamItemDubboService;
import com.ego.pojo.TbItem;
import com.ego.pojo.TbItemDesc;
import com.ego.pojo.TbItemParam;
import com.ego.pojo.TbItemParamItem;
import com.ego.rabbitmq.send.PublishMessage;
import com.ego.service.TBItemService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TBItemServiceImpl  implements TBItemService {


    @DubboReference
    private TBItemDubboService tbItemDubboService;

    @DubboReference
    private TbItemParamItemDubboService tbItemParamItemDubboService;


    @Autowired
    private PublishMessage publishMessage;


    @Value("${ego.operation.exchange}")
    private String exchange;

    @Value("${ego.operation.solrInsertRoutingKey}")
    private String routingKey;

    @Value("${ego.operation.solrDeleteRoutingKey}")
    private String solrDeleteRoutingKey;

    @Override
    public EasyUIDatagrid showItem(int page, int size) {

        //消费 tbItemDubboService 的 查询分页方法
        PageInfo<TbItem> tbItemPageInfo = tbItemDubboService.selectTbItemByPage(page, size);

        //获取结果集
        List<TbItem> items = tbItemPageInfo.getList();

        //获取总数
        long total = tbItemPageInfo.getTotal();

        EasyUIDatagrid easyUIDatagrid = new EasyUIDatagrid(items, total);

        return easyUIDatagrid;
    }

    @Override
    public EgoResult updateItemStatusByIds(long[] ids,int status) {

        try {
           tbItemDubboService.updateItemStatusByIds(ids, status);

           //上架
           if(status==1){
               //使用RabbitMQ发送消息，更新Solr索引引擎数据
               publishMessage.asyncSenderMessage(exchange,routingKey,ids);
           }else if(status == 2 || status ==3){
               publishMessage.asyncSenderMessage(exchange,solrDeleteRoutingKey,ids);
           }

           return EgoResult.ok();
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }

    }

    @Override
    public EgoResult insert(TbItem tbItem, String desc,String itemParams) {

        long itemId = IDUtils.genItemId();
        //设置商品id
        tbItem.setId(itemId);


        //创建商品描述对象
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);

        //创建商品规格参数对象
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setParamData(itemParams);
        try {
            int row = tbItemDubboService.insert(tbItem, tbItemDesc,tbItemParamItem);

            if (row == 1){
                //使用RabbitMQ发送消息，更新Solr索引引擎数据
                publishMessage.asyncSenderMessage(exchange,routingKey,itemId);

                return EgoResult.ok();
            }
        } catch (DaoException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("新增商品失败!");
    }

    @Override
    public EgoResult update(TbItem tbItem, String desc,String itemParams,long itemParamId) {
        //创建商品描述对象
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);

        //创建商品规格参数对象
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setId(itemParamId);
        tbItemParamItem.setParamData(itemParams);

        try {
            int row = tbItemDubboService.update(tbItem, tbItemDesc,tbItemParamItem);
            if (row == 1){

                //使用RabbitMQ发送消息，更新Solr索引引擎数据
                publishMessage.asyncSenderMessage(exchange,routingKey,tbItem.getId());

                return EgoResult.ok();
            }
        } catch (DaoException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("修改商品失败!");
    }

    @Override
    public EgoResult itemParmItemQueryByItemId(long itemId) {

        try {
            TbItemParamItem tbItemParamItem = tbItemParamItemDubboService.selectByItemId(itemId);

            return EgoResult.ok(tbItemParamItem);
        } catch (Exception e) {
            e.printStackTrace();
            return EgoResult.error("查询规格参数失败");
        }
    }
}
